package com.jyxd.web.task;

import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.service.assessment.basic.MonitorLifeDataService;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.service.assessment.basic.QuartzTimeService;
import com.jyxd.web.util.CompareTimeUtil;
import com.jyxd.web.util.SpringUtil;
import com.jyxd.web.util.UUIDUtil;
import org.quartz.CronExpression;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.jyxd.web.util.DateUtil.getLaterHoursDate;
import static com.jyxd.web.util.DateUtil.yyyyMMddHHmmssSdfToString;

/**
 * 定时删除监护仪历史数据任务
 */
public class DeleteMonitorLifeDataJob implements Job {

    private static Logger logger = LoggerFactory.getLogger(DeleteMonitorLifeDataJob.class);
    //定义一个成员变量
    private static QuartzTask quartzTask;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        System.out.println("开始执行删除监护仪数据任务");
        QuartzTime quartzTime = new QuartzTime();
        quartzTime.setStartTime(new Date());
        ServletContext context;
        CronExpression expression;
        try {
            //获取service
            context = (ServletContext) jobExecutionContext.getScheduler().getContext().get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTimeService quartzTimeService = (QuartzTimeService) cxt.getBean("quartzTimeService");
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");
            MonitorLifeDataService monitorLifeDataService = SpringUtil.getBean(MonitorLifeDataService.class);
            if (Objects.nonNull(monitorLifeDataService)) {
                Map<String, Object> map = new HashMap<>();
                map.put("startTime", "2021-01-01 00:00:00");
                //改为删除两天之前的数据，由于数据太多导致前台页面直接卡死
                Date laterHoursDate = getLaterHoursDate(new Date(), -48L);
                String endTime = yyyyMMddHHmmssSdfToString(laterHoursDate);
                map.put("endTime", endTime);
                monitorLifeDataService.deleteData(map);
            }
            //操作定时任务执行记录表
            Map<String, Object> map = new HashMap<>();
            map.put("jobName", "MonitorLifeDataJob");
            map.put("jobGroup", "MonitorLifeDataJob");
            quartzTask = quartzTaskService.queryDataByNameAndGroup(map);
            expression = new CronExpression(quartzTask.getCron());
            quartzTime.setNextTime(expression.getNextValidTimeAfter(quartzTime.getStartTime()));
            quartzTime.setQuartzTaskId(quartzTask.getId());
            quartzTime.setId(UUIDUtil.getUUID());
            quartzTime.setEndTime(new Date());
            quartzTime.setTimeLength(String.valueOf(CompareTimeUtil.calculatetimeGapSecond(quartzTime.getStartTime(), quartzTime.getEndTime())));
            quartzTime.setStatus(1);
            quartzTimeService.insert(quartzTime);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
